package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.EnterpriseScheduleCarBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EnterpriseScheduleCarMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EnterpriseScheduleCarCreateDto;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseScheduleCarCountDto;
import com.molichuxing.services.infrastructure.service.EnterpriseScheduleCarService;
import com.molichuxing.services.property.EnterpriseScheduleStateEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 企业排期
 *
 * @author zhaopenghui
 * @date 2020年6月3日
 */
@Service("enterpriseScheduleCarService")
public class EnterpriseScheduleCarServiceImpl implements EnterpriseScheduleCarService {

    @Resource
    private EnterpriseScheduleCarMapper enterpriseScheduleCarMapper;

    /**
     * 订单车辆排期信息
     *
     * @param scheduleIdList
     * @return
     */
    @Override
    public Map<Integer, EnterpriseScheduleCarCountDto> getScheduleCarMap(List<Integer> scheduleIdList) {
        if (null == scheduleIdList || scheduleIdList.size() < 1) {
            return null;
        }

        List<EnterpriseScheduleCarCountDto> listByScheduleId = enterpriseScheduleCarMapper.getCarCountByScheduleId(scheduleIdList);
        if (null == listByScheduleId || listByScheduleId.size() < 1) {
            return null;
        }
        return listByScheduleId.stream().collect(Collectors.toMap(EnterpriseScheduleCarCountDto::getScheduleId, a -> a, (k1, k2) -> k1));
    }

    /**
     * 已付款
     *
     * @param scheduleIdList
     * @return
     */
    @Override
    public Map<Integer, EnterpriseScheduleCarCountDto> getPayableMap(List<Integer> scheduleIdList) {
        if (null == scheduleIdList || scheduleIdList.size() < 1) {
            return null;
        }

        List<EnterpriseScheduleCarCountDto> listByScheduleId = enterpriseScheduleCarMapper.getAllPayableByScheduleId(scheduleIdList);
        if (null == listByScheduleId || listByScheduleId.size() < 1) {
            return null;
        }
        return listByScheduleId.stream().collect(Collectors.toMap(EnterpriseScheduleCarCountDto::getScheduleId, a -> a, (k1, k2) -> k1));
    }

    /**
     * 新增首付车辆排期
     *
     * @param createDtos 新增信息
     * @return boolean
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean createBatch(List<EnterpriseScheduleCarCreateDto> createDtos) {
        return enterpriseScheduleCarMapper.createBatch(Convert.toEnterpriseScheduleCarBean(createDtos));
    }

    /**
     * 终止订单车辆排期
     *
     * @param orderCode 订单号
     * @param carId     车辆id
     * @return boolean
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean modifyTermination(Integer carId) {
        List<EnterpriseScheduleCarBean> scheduleCarBeans = enterpriseScheduleCarMapper.getByCar(carId, EnterpriseScheduleStateEnum.NO.getValue());
        if (scheduleCarBeans == null || scheduleCarBeans.isEmpty()) {
            return true;
        }

        return enterpriseScheduleCarMapper.modifyTermination(carId,
                EnterpriseScheduleStateEnum.NO.getValue(),
                EnterpriseScheduleStateEnum.TERMINATION.getValue()) > 0;
    }

    /**
     * 还款修改状态
     *
     * @param scheduleId 排期id
     * @return boolean
     */
    @Override
    public boolean modifyRepay(Integer scheduleId) {
        return enterpriseScheduleCarMapper.modifyRepay(scheduleId,
                EnterpriseScheduleStateEnum.ALREADY.getValue(),
                EnterpriseScheduleStateEnum.NO.getValue()) > 0;
    }

}
